<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_login extends CI_Controller
{
    public function index(){
        $this->load->view('user_login');
    }

    // 登录
    function login()
    {

        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', '用户名', 'required');
        $this->form_validation->set_rules('password', '密码', 'required');

        if ($this->form_validation->run() == FALSE) {
            $controller = $this->uri->segment(1,1);
            if($controller=='main'){
                redirect('main/index');
            }else{
                redirect($controller.'/index');
            }

        } else {
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $this->load->model('user_mod');
            $where = array(
                'username' => $username,
                'password' => md5($password)
            );
            $res = $this->user_mod->getUser($where);
            if ($res) {
                $array = array(
                    'username' => $res['username'],
                    'uid' => $res['uid'],
                    'islogin' => 1
                );
                $this->session->set_userdata($array);
                redirect('main/index');
            } else {
                redirect('main/index');
            }
        }
    }
}